package cn.itsource.exam.service.impl;

import cn.itsource.exam.domain.Role;
import cn.itsource.exam.mapper.RoleMapper;
import cn.itsource.exam.query.RoleQueryVo;
import cn.itsource.exam.service.IRoleService;
import cn.itsource.exam.util.PageList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void deleteRole(Long roleId) {
        roleMapper.deleteRole(roleId);
    }

    @Override
    public void insertRole(Role role) {
        roleMapper.insertRole(role);
    }

    @Override
    public void updateRole(Role role) {
        roleMapper.updateRole(role);
    }

    @Override
    public Role selectRoleByRoleId(Long  roleId) {
        return roleMapper.selectRoleByRoleId(roleId);
    }

    @Override
    public PageList<Role> selectRole(RoleQueryVo roleQueryVo) {
        PageList<Role> pageList=new PageList<Role>();
        Integer total=roleMapper.selectCount(roleQueryVo);
        if(total==null){
          return pageList;
        }
        List<Role> roles = roleMapper.selectRoleList(roleQueryVo);
        pageList.setList(roles);
        pageList.setTotal(total);
        return  pageList;
    }

    @Override
    public Role selectRoleByName(Role role) {
        if(role==null){
            return null;
        }
        return roleMapper.selectRoleByRoleName(role.getName());
    }

    @Override
    public void batchDeleteRole(Long[] roleIds) {
        roleMapper.batchDeleteRole(roleIds);
    }

    @Override
    public void addUserRole(Long userId, String userType) {
        if(userId==null||StringUtils.isBlank(userType )){
            return;
        }
        //学生
        if("1".equals(userType)){
            //Role role=roleMapper.getRoleByCode("student");
            roleMapper.insertUserRole(userId,4);
        }else if("0".equals(userType)){//老师
            roleMapper.insertUserRole(userId,2);
        }
    }
}
